package sys.database;

import java.sql.*;

public class DBWrapper
{
    public DBWrapper(String DBHost, String DBName, String user, String pass)
    {
        this.DBHost = DBHost;
        this.DBName = DBName;
        this.user = user;
        this.pass = pass;
    }

    public void connect()
    {
        try 
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://" + DBHost + "/" + DBName, user, pass);

            if(!con.isClosed())
                System.out.println("Successfully connected to dname = " + DBName + ", user = " + user);

        } 
        catch(Exception e) 
        {
            System.err.println("Exception: " + e.getMessage());
        } 
    }

    public ResultSet select(String table, String where)
    {
        ResultSet rs = null;
        try
        {
            statement = con.createStatement();
            String query = "SELECT * FROM " + table + " WHERE " + where;
            System.out.println(query);
            rs = statement.executeQuery(query);
        }
        catch (SQLException e) 
        {
            System.err.println("Error while selecting data: " + e.getMessage());
            rs = null;
        }
        return rs;
    }
    
    public void insert(String table, String values)
    {
        try
        {
            statement = con.createStatement();
            String query = "INSERT INTO " + table + " VALUES (" + values + ")";
            System.out.println(query);
            statement.execute(query);
        }
        catch (SQLException e) 
        {
            System.err.println("Error while inserting data: " + e.getMessage());
        }
    } 

    public void insert(String table, String fields, String values)
    {
        try
        {
            statement = con.createStatement();
            String query = "INSERT INTO " + table + "(" + fields + ") VALUES (" + values + ")";
            System.out.println(query);
            statement.execute(query);
        }
        catch (SQLException e) 
        {
            System.err.println("Error while inserting data: " + e.getMessage());
        }
    } 

    public void delete(String table, String where)
    {
        try
        {
            statement = con.createStatement();
            String query = "DELETE FROM " + table + " WHERE " + where;
            System.out.println(query);
            statement.executeUpdate(query);
        }
        catch (SQLException e) 
        {
            System.err.println("Error while deleting data: " + e.getMessage());
        }
    } 

    public void update(String table, String field, String newValue, String where)
    {
        try
        {
            statement = con.createStatement();
            String query = "UPDATE " + table + " SET " + field + "=" + newValue + " WHERE " + where;
            System.out.println(query);
            statement.executeUpdate(query);
        }
        catch (SQLException e) 
        {
            System.err.println("Error while updating data: " + e.getMessage());
        }
    }

    public void test()
    {
        try
        {
            insert("PayingSystem", "Address, Password", "'webmoney', 'pass1'");
            insert("PayingSystem", "Address, Password", "'yandexmoney', 'pass2'");
            update("PayingSystem", "Address", "'webmoney2'", "Address = 'webmoney'");
            ResultSet rs = select("PayingSystem", "Address = 'pass1'");
            while(rs.next())
            {
                System.out.println("system = " + rs.getString(1) + " ,address = " + rs.getString(2));
            }
            rs.close();
        }
        catch(SQLException e)
        {
            System.err.println("Error while testing: " + e.getMessage());
        }
    }

    public void disconnect()
    {
        try
        {
            statement.close();
            if (con != null)
                con.close();
        }
        catch(SQLException e) 
        {
            System.err.println("Error while disconnecting: " + e.getMessage());
        }
    }

	public Connection getConnection()
	{
		return con;
	}

    private String DBHost, DBName, user, pass;
    private Connection con;
    private Statement statement;
}
